package org.lql.algo.codecrush.week010;

import org.lql.algo.common.TreeNode;

/**
 * @author: liangqinglong
 * @date: 2025-08-30 15:22
 * @description: 面试题 17.12. BiNode <a href="https://leetcode.cn/problems/binode-lcci/description/">...</a>
 **/
public class ConvertBiNode {

	private TreeNode prev = new TreeNode(-1); // 虚拟头节点
	private TreeNode dummy = prev;

	public TreeNode convertBiNode(TreeNode root) {
		inorder(root);
		return dummy.right; // 返回链表的头
	}

	private void inorder(TreeNode node) {
		if (node == null) {
			return;
		}

		inorder(node.left);

		// 把当前节点挂到链表末尾
		prev.right = node;
		node.left = null;  // 必须清空左指针
		prev = node;

		inorder(node.right);
	}
}
